package com.example;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Date;

@SpringBootTest
public class LearnDateUtil {

    @Test
    public void test1() {
        // 当前完整时间：date = 2025-04-09 09:31:15
        DateTime date = DateUtil.date();
        System.out.println("date = " + date);
        // 当前日期：today = 2025-04-09
        String today = DateUtil.today();
        System.out.println("today = " + today);
        // 当前时间字符串：now = 2025-04-09 09:31:15
        String now = DateUtil.now();
        System.out.println("now = " + now);
        System.out.println("=========================");

        // 时间字符串
        String str = "2025-04-01 12:00:00";
        System.out.println("str = " + str);
        // 格式转换
        DateTime parse = DateUtil.parse(str);
        System.out.println("parse = " + parse);
        // 格式化日期
        String format = DateUtil.format(parse, "yyyy/MM/dd HH:mm:ss");
        System.out.println("format = " + format);
        System.out.println("=========================");
    }

    @Test
    public void test2() {
        // 当前时间
        DateTime date = DateUtil.date();
        System.out.println("date = " + date);
        // 当前时间天数减一
        DateTime offsetDay = DateUtil.offsetDay(date, -1);
        System.out.println("offsetDay = " + offsetDay);
        // 当前时间小时加二
        DateTime offsetHour = DateUtil.offsetHour(date, 2);
        System.out.println("offsetHour = " + offsetHour);
    }

    @Test
    public void test3() {
        DateTime start = DateUtil.parse("2025-04-01 12:00:00");
        DateTime end = DateUtil.parse("2025-04-09 23:00:00");
        // 两个时间之间的差值
        long days = DateUtil.betweenDay(start, end, true);
        System.out.println("间隔天数：" + days);

    }

    @Test
    public void test4() {
        DateTime beginOfMonth = DateUtil.beginOfMonth(DateUtil.date());
        System.out.println("beginOfMonth = " + beginOfMonth);
        DateTime endOfMonth = DateUtil.endOfMonth(DateUtil.date());
        System.out.println("endOfMonth = " + endOfMonth);
    }

    @Test
    public void test5() {
        boolean leapYear = DateUtil.isLeapYear(2024);
        System.out.println("今年是否是闰年：" + leapYear);
    }

    @Test
    public void test6() {
        String birthday = "2002-04-18";
        DateTime date = DateUtil.parse(birthday);
        System.out.println("date = " + date);
        long days = DateUtil.betweenDay(date, DateUtil.date(), true);
        System.out.println("你已经活了：" + days + "天！");
        long month = DateUtil.betweenMonth(date, DateUtil.date(), true);
        System.out.println("你已经活了：" + month + "月！");
    }

    @Test
    public void test7() {
        String birthday = "2002-04-18";
        DateTime date = DateUtil.parse(birthday);
        System.out.println("date = " + date);
        String weekDay = getWeekDay(date);
        System.out.println("weekDay = " + weekDay);
    }

    public static String getWeekDay(DateTime date) {
        return DateUtil.dayOfWeekEnum(date).toChinese();
    }

}
